/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.siteEncheres.client.utilisateur;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.PaddedPanel;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.ToolTip;
import com.gwtext.client.widgets.form.DateField;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.MultiFieldPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.TimeField;
import com.gwtext.client.widgets.form.VType;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;
import domain.Utilisateur;

/**
 *
 * @author Francois
 */
public class IhmInscrireUtilisateur extends Panel{

    private Label lblNom = new Label("Nom");
    private Label lblPrenom = new Label("Prénom");
    private Label lblDateNaissance = new Label("Date de naissance");
    private Label lblRue = new Label("Adresse");
    private Label lblCodePostal = new Label("Code postal");
    private Label lblVille = new Label("Ville");
    private Label lblNumTel = new Label("Numéro de téléphone");
    private Label lblMail = new Label("E-mail");
    private Label lblMailVerif = new Label("Veuillez ressaisir votre e-mail");
    private Label lblLogin = new Label("Login");
    private Label lblMdp = new Label("Mot de passe");
    private Label lblMdpVerif = new Label("Veuillez ressaisir votre mot de passe");
    private Label lblUtilisateur = new Label();
    
    private TextBox txtNom = new TextBox();
    private TextBox txtPrenom = new TextBox();
    private TextBox txtDateNaissance = new TextBox();
    private TextBox txtRue = new TextBox();
    private TextBox txtCodePostal = new TextBox();
    private TextBox txtVille = new TextBox();
    private TextBox txtNumTel = new TextBox();
    private TextBox txtMail = new TextBox();
    private TextBox txtMailVerif = new TextBox();
    private TextBox txtLogin = new TextBox();
    private TextField txtMdp = new TextField();
    private TextField txtMdpVerif = new TextField();
    
    private Utilisateur u = null;
    private Button btnEnregistrer = new Button("Enregistrer");
    final AsyncCallback callback = new AsyncCallback() {

        public void onSuccess(Object result) {
            txtNom.setText("");
            txtPrenom.setText("");
            txtDateNaissance.setText("");
            txtRue.setText("");
            txtCodePostal.setText("");
            txtVille.setText("");
            txtNumTel.setText("");
            txtMail.setText("");
            txtMailVerif.setText("");
            txtLogin.setText("");
            txtMdp.setValue("");
            txtMdpVerif.setValue("");
            
        }

        public void onFailure(Throwable caught) {
            MessageBox.alert(caught.getMessage());
        }
    };
    
    public IhmInscrireUtilisateur() {
        setBorder(false);  
        setPaddings(10);
        setAutoScroll(true);
        setFrame(true);
        setLayout(new VerticalLayout(20));
        
        //////////// panel Nom Prenom /////////////////
        Panel panelNP = new Panel();
        panelNP.setBorder(false);  
        panelNP.setLayout(new HorizontalLayout(20));
        
        Panel pNom = new Panel();
        pNom.setLayout(new VerticalLayout());      
        pNom.add(lblNom);
        pNom.add(txtNom);

        Panel pPrenom = new Panel();
        pPrenom.setLayout(new VerticalLayout());      
        pPrenom.add(lblPrenom);
        pPrenom.add(txtPrenom);

        panelNP.add(pNom);
        panelNP.add(pPrenom);
        add(panelNP);
        //////////////////////////////////////////////
        
        
        //////////// panel Date naissance /////////////////
        Panel panelDateNaiss = new Panel();
        panelDateNaiss.setBorder(false);  
        panelDateNaiss.setLayout(new VerticalLayout());
        panelDateNaiss.add(lblDateNaissance);
        panelDateNaiss.add(txtDateNaissance);
        add(panelDateNaiss);
        //////////////////////////////////////////////
        
        //////////// panel Adresse /////////////////
        Panel panelAdresse = new Panel();
        panelAdresse.setBorder(false);  
        panelAdresse.setLayout(new VerticalLayout());
        
        Panel panelRue = new Panel();
        panelRue.setLayout(new VerticalLayout());
        panelRue.add(lblRue);
        panelRue.add(txtRue);
        
        Panel panelCpVille = new Panel();
        panelCpVille.setLayout(new HorizontalLayout(20));
        
        Panel panelCp = new Panel();
        panelCp.setLayout(new VerticalLayout());
        panelCp.add(lblCodePostal);
        panelCp.add(txtCodePostal);
        
        Panel panelVille = new Panel();
        panelVille.setLayout(new VerticalLayout());
        panelVille.add(lblVille);
        panelVille.add(txtVille);
        
        panelCpVille.add(panelCp);
        panelCpVille.add(panelVille);
        
        panelAdresse.add(panelRue);
        panelAdresse.add(panelCpVille);
        add(panelAdresse);
        //////////////////////////////////////////////
        
        //////////// panel telephone /////////////////
        Panel panelTel = new Panel();
        panelTel.setBorder(false);  
        panelTel.setLayout(new VerticalLayout());
        
        panelTel.add(lblNumTel);
        panelTel.add(txtNumTel);
        add(panelTel);
        //////////////////////////////////////////////
        
        //////////// panel Mail /////////////////
        Panel panelMail = new Panel();
        panelMail.setBorder(false);  
        panelMail.setLayout(new VerticalLayout());
        
        panelMail.add(lblMail);
        panelMail.add(txtMail);
        add(panelMail);
        //////////////////////////////////////////////
        
        //////////// panel Mail verif/////////////////
        Panel panelMailVerif = new Panel();
        panelMailVerif.setBorder(false);  
        panelMailVerif.setLayout(new VerticalLayout());
        
        panelMailVerif.add(lblMailVerif);
        panelMailVerif.add(txtMailVerif);
        add(panelMailVerif);
        //////////////////////////////////////////////
        
        //////////// panel Login/////////////////
        Panel panelLogin = new Panel();
        panelLogin.setBorder(false);  
        panelLogin.setLayout(new VerticalLayout());
        
        panelLogin.add(lblLogin);
        panelLogin.add(txtLogin);
        add(panelLogin);
        //////////////////////////////////////////////
        
        //////////// panel Mdp /////////////////
        Panel panelMdp = new Panel();
        panelMdp.setBorder(false);  
        panelMdp.setLayout(new VerticalLayout());
        
        panelMdp.add(lblMdp);
        panelMdp.add(txtMdp);
        add(panelMdp);
        //////////////////////////////////////////////
        
        //////////// panel Mdp verif /////////////////
        Panel panelMdpVerif = new Panel();
        panelMdpVerif.setBorder(false);  
        panelMdpVerif.setLayout(new VerticalLayout());
        
        panelMdpVerif.add(lblMdpVerif);
        panelMdpVerif.add(txtMdpVerif);
        add(panelMdpVerif);
        //////////////////////////////////////////////
        
        add(btnEnregistrer);
        add(lblUtilisateur);
        

        btnEnregistrer.addClickListener(new ClickListener() {
            public void onClick(Widget w) {
                if ((txtMdp.getText().compareTo(txtMdpVerif.getText()) == 0) && (txtMail.getText().compareTo(txtMailVerif.getText()) == 0)) {
                    u = new Utilisateur();
                    u.setNom(txtNom.getText());
                    u.setPrenom(txtPrenom.getText());
                    u.setDateNaissance(null);
                    u.setRue(txtRue.getText());
                    u.setCodePostal(txtCodePostal.getText());
                    u.setVille(txtVille.getText());
                    u.setTelephone(txtNumTel.getText());
                    u.setEmail(txtMail.getText());
                    u.setLogin(txtLogin.getText());
                    u.setMotDePasse(txtMdp.getText());
                    getService().inscrireUtilisateur(u, callback);
                } else {
                    MessageBox.alert("Les 2 mots de passe ou mails sont différents.\nVeuillez les ressaisir.");
                    txtMail.setText("");
                    txtMailVerif.setText("");               
                    txtMdp.setValue("");
                    txtMdpVerif.setValue("");                            
                }

            }
        });
        
        
        
        
        
        
        
        /*final FormPanel formPanel = new FormPanel();  
        formPanel.setFrame(true);  
        formPanel.setBorder(true);  
        formPanel.setTitle("Inscription");  
        formPanel.setWidth(550);  
        formPanel.setLabelWidth(100);  
        formPanel.setUrl("save-form.php");  
  
        TextField firstName = new TextField("First / Last", "first", 110, "Sanjiv");  
  
        TextField lastName = new TextField("Last Name", "last", 110, "Jivan");  
        lastName.setHideLabel(true);  
  
        MultiFieldPanel namePanel = new MultiFieldPanel();  
        namePanel.addToRow(firstName, 220);  
        namePanel.addToRow(lastName, new ColumnLayoutData(1));  
        formPanel.add(namePanel);  
  
        TextField company = new TextField("Company", "company", 222);  
        formPanel.add(company);  
  
        MultiFieldPanel dateTimePanel = new MultiFieldPanel();  
        dateTimePanel.addToRow(new DateField("Appointment", "appointment", 140), 250);  
        TimeField timeField = new TimeField();  
        timeField.setHideLabel(true);  
        timeField.setWidth(80);  
        dateTimePanel.addToRow(timeField, new ColumnLayoutData(1));  
        formPanel.add(dateTimePanel);  
  
        TextField email = new TextField("Email", "email", 222);  
        email.setVtype(VType.EMAIL);  
        formPanel.add(email);  
  
        Image image = new Image("images/captcha.jpg");  
        Panel imagePanel = new Panel();  
        imagePanel.setBorder(false);  
        imagePanel.add(image);  
  
        ToolTip imageToolTip = new ToolTip("Tooltip on an Image");  
        imageToolTip.applyTo(image.getElement());  
  
        formPanel.add(new PaddedPanel(imagePanel, 0, 110, 0, 5));  
  
        MultiFieldPanel captchaPanel = new MultiFieldPanel();  
        TextField captchaField = new TextField("Captcha", "captcha", 110);  
        captchaPanel.addToRow(captchaField, 220);  
  
        ToolTip fieldToolTip = new ToolTip("Tooltip on a Field.");  
        fieldToolTip.applyTo(captchaField);  
  
        Image info = new Image("images/silk/information.gif");  
  
        ToolTip tooltip = new ToolTip();  
        tooltip.setHtml("A <b>CAPTCHA</b> is a challenge-response test to determine whether " +  
                            "the user is human.");  
        tooltip.setWidth(150);  
        tooltip.applyTo(info.getElement());  
  
        captchaPanel.addToRow(info, new ColumnLayoutData(1));  
        formPanel.add(captchaPanel);  
  
        add(formPanel);*/
    }
    
    public static GwtUtilisateurAsync getService() {
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of the interface. The cast is always safe because the
        // generated proxy implements the asynchronous interface automatically.

        return GWT.create(GwtUtilisateur.class);
    }
}